﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.Linq;
using System.Linq;
using System.Web;
using System.Data.Linq.Mapping;

namespace SubSystem.Models.ObjectStore
{
    public class ArticleSqlObjectStore : IObjectStore<IArticle>
    {
        private DataContext context;

        public ArticleSqlObjectStore()
        {
            this.context = new DataContext(Settings.ConnectionString);
        }

        public IEnumerable<IArticle> Fetch()
        {
            return context
                .GetTable<MappedArticle>()
                .Cast<IArticle>()
                .AsEnumerable();
        }

        public bool Save(IArticle item)
        {
            var table = context.GetTable<MappedArticle>();
            var existing = table.Where(a => a.Id == item.Id);
            if (existing.Count() > 0)
            {
                existing
                    .FirstOrDefault()
                    .UpdateFrom(item);
            }
            else
            {
                table.InsertOnSubmit(new MappedArticle(item));
            }

            try
            {
                context.SubmitChanges();
            }
            catch (Exception ex)
            {
                throw new Exception("Save failed", ex);
            }

            return true;
        }
    }

    [Table(Name = "Article")]
    public class MappedArticle : IArticle
    {
        public MappedArticle()
        {
        }

        public MappedArticle(IArticle article)
        {
            Id = article.Id;
            Author = article.Author;
            Content = article.Content;
            Tags = article.Tags; // - needs to use Collection
        }

        public MappedArticle UpdateFrom(IArticle item)
        {
            Content = item.Content;
            Author = item.Author;
            Tags = item.Tags;
            return this;
        }

        private string tags = string.Empty;

        [Column(Name = "Id", IsPrimaryKey = true)]
        public Guid Id { get; set; }

        [Column(Name = "Content")]
        public string Content { get; set; }

        [Column(Name = "Author")]
        public Guid Author { get; set; }

        [Column(Storage = "tags", Name = "Tags")]
        public string TagsString
        {
            get
            {
                return tags;
            } 
            set
            {
                tags = value;
            }
        }

        public IEnumerable<string> Tags 
        {
            get
            {
                return tags.Split(',').AsEnumerable();
            }

            set
            {
                tags = string.Empty;
                foreach (var s in value)
                {
                    tags += s + ",";
                }

                if (tags.Length > 0)
                {
                    tags = tags.Remove(tags.Length - 1);
                }
            }
        }

    }
}